<?php
class ModelCategoryProduct extends Model 
{
	public function getList($where = "")
	{
		$query = $this->db->query("Select `product`.*, groupname from `product`,`product_group` where `product`.groupid = `product_group`.groupid ".$where);
		return $query->rows;
	}
	
	public function getItem($productid)
	{
		$query = $this->db->query("Select * from `product` where productid = '".$productid."'");
		return $query->row;
	}
	
	public function getByBarcode($barcode)
	{
		$query = $this->db->query("Select * from `product` where barcode = '".$barcode."'");
		return $query->row;
	}
	
	public function nextbarcodeID($prefix)
	{
		return $this->db->getNextIdVarChar("product","barcode",$prefix);
	}
	
	public function insert($data)
	{
		$productid=$this->db->escape(@$data['productid']);
		$productname=$this->db->escape(@$data['productname']);
		$barcode=$this->db->escape(@$data['barcode']);
		$line1=$this->db->escape(@$data['line1']);
		$line2=$this->db->escape(@$data['line2']);
		$groupid=$this->db->escape(@$data['groupid']);
		$ignorestock=$this->db->escape(@$data['ignorestock']);
		$notifyqty=(float)(@$data['notifyqty']);
		$unit=$this->db->escape(@$data['unit']);
		$imageid=$this->db->escape(@$data['imageid']);
		$imagepath=$this->db->escape(@$data['imagepath']);
		$subimages=$this->db->escape(@$data['subimages']);
		$importqty=(float)(@$data['importqty']);
		$exportqty=(float)(@$data['exportqty']);
		$instock=(float)(@$data['instock']);
		$netprice=(float)(@$data['netprice']);
		$saleprice=(float)(@$data['saleprice']);
		$description=$this->db->escape(@$data['description']);
		$status=$this->db->escape(@$data['status']);
		
		$field=array(
						'productname',
						'barcode',
						'line1',
						'line2',
						'groupid',
						'ignorestock',
						'notifyqty',
						'unit',
						'imageid',
						'imagepath',
						'subimages',
						'importqty',
						'exportqty',
						'instock',
						'netprice',
						'saleprice',
						'description',
						'status'
					);
		$value=array(
						$productname,
						$barcode,
						$line1,
						$line2,
						$groupid,
						$ignorestock,
						$notifyqty,
						$unit,
						$imageid,
						$imagepath,
						$subimages,
						$importqty,
						$exportqty,
						$instock,
						$netprice,
						$saleprice,
						$description,
						$status
					);
		return $this->db->insertData("product",$field,$value);
	}
	
	public function update($data)
	{
		$productid=$this->db->escape(@$data['productid']);
		$productname=$this->db->escape(@$data['productname']);
		$barcode=$this->db->escape(@$data['barcode']);
		$line1=$this->db->escape(@$data['line1']);
		$line2=$this->db->escape(@$data['line2']);
		$groupid=$this->db->escape(@$data['groupid']);
		$ignorestock=$this->db->escape(@$data['ignorestock']);
		$notifyqty=(float)(@$data['notifyqty']);
		$unit=$this->db->escape(@$data['unit']);
		$imageid=$this->db->escape(@$data['imageid']);
		$imagepath=$this->db->escape(@$data['imagepath']);
		$subimages=$this->db->escape(@$data['subimages']);
		$importqty=(float)(@$data['importqty']);
		$exportqty=(float)(@$data['exportqty']);
		$instock=(float)(@$data['instock']);
		$netprice=(float)(@$data['netprice']);
		$saleprice=(float)(@$data['saleprice']);
		$description=$this->db->escape(@$data['description']);
		$status=$this->db->escape(@$data['status']);
		
		$field=array(
						'productname',
						'barcode',
						'line1',
						'line2',
						'groupid',
						'ignorestock',
						'notifyqty',
						'unit',
						'imageid',
						'imagepath',
						'subimages',
						'importqty',
						'exportqty',
						'instock',
						'netprice',
						'saleprice',
						'description',
						'status'
					);
		$value=array(
						$productname,
						$barcode,
						$line1,
						$line2,
						$groupid,
						$ignorestock,
						$notifyqty,
						$unit,
						$imageid,
						$imagepath,
						$subimages,
						$importqty,
						$exportqty,
						$instock,
						$netprice,
						$saleprice,
						$description,
						$status
					);
					
		$where="productid = '".$productid."'";
		$this->db->updateData("product",$field,$value,$where);
	}	
			
	public function delete($productid)
	{
		$productid=$this->db->escape(@$productid);
		$where="productid = '".$productid."'";
		$this->db->deleteData('product',$where);
	}
	
	public function deletes($data)
	{
		foreach($data as $productid)
		{
			$this->delete($productid);
		}		
	}
	
	public function validatebarcode($barcode, $oldbarcode)
	{
		$query = $this->db->query("Select * from `product` where barcode not like '".$oldbarcode."' and barcode like '".$barcode."'");
		if(count($query->rows) > 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
}

?>